package com.banshuibao.bsb_android.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.AddressModel;
import com.banshuibao.bsb_android.model.ExpertTreeModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2018/1/11.
 */

public class SelectPopupWindowUtil {
    Context context;

    private SelectPopupWindowUtil.ItemOnClickListener itemOnClickListener;
    private String tag;

    private PopupWindow popupWindow;

    public SelectPopupWindowUtil(Context context) {
        this.context = context;
    }
    public PopupWindow getPopupWindow(final List<ExpertTreeModel.DataBean.ChildrenBean> list, final List<AddressModel> addList, final String tag) {
        View view = View.inflate(context, R.layout.expert_tree_popuwindows_layout, null);
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
        popupWindow.setOutsideTouchable(true);
        //设置SelectPicPopupWindow的View
        popupWindow.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体可点击
        popupWindow.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        popupWindow.setAnimationStyle(R.style.PopupWindowns_animation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(dw);


        if (view != null) {
            this.tag = tag;
            if (view.findViewById(R.id.listView) != null) {
                final ListView listView = view.findViewById(R.id.listView);
                View v = View.inflate(context, R.layout.expert_tree_item_layout, null);
                TextView tv = v.findViewById(R.id.title);
                listView.addHeaderView(v);
                final Adapter adapter = new Adapter();
                if(tag.equals("tree")){
                    tv.setText("无");
                    adapter.setTreeData(list);
                }else if(tag.equals("")){
                    listView.removeHeaderView(v);
                    adapter.setAddressData(addList);
                }
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        if(itemOnClickListener!=null){
                            if(tag.equals("tree")){
                                if(i==0){
                                    itemOnClickListener.getContent("无","");
                                }else {
                                    itemOnClickListener.getContent(list.get(i - 1).getName(), list.get(i - 1).getUuid());
                                }
                            }else if(tag.equals("")){
                                itemOnClickListener.getContent(addList.get(i).getName(), addList.get(i).getUuid());
                            }

                        }
                    }
                });

                View popup_bg = view.findViewById(R.id.popup_bg);
                popup_bg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow.isShowing()) {
                            popupWindow.dismiss();
                        }
                    }
                });
            }
        }

        return popupWindow;
    }

    class Adapter extends BaseAdapter {
        List<BaseModel> listAll = new ArrayList<>();

        public void setTreeData(List<ExpertTreeModel.DataBean.ChildrenBean> list) {
            listAll.clear();
            for (int i = 0; i < list.size(); i++) {
                BaseModel baseModel = new BaseModel();
                baseModel.setData(list.get(i));
                listAll.add(baseModel);
            }
            notifyDataSetChanged();
        }

        public void setAddressData(List<AddressModel> list){
            listAll.clear();
            for (int i = 0; i < list.size(); i++) {
                BaseModel baseModel = new BaseModel();
                baseModel.setData(list.get(i));
                listAll.add(baseModel);
            }
            notifyDataSetChanged();
        }


        @Override
        public int getCount() {
            return listAll != null ? listAll.size() : 0;
        }

        @Override
        public Object getItem(int i) {
            return listAll.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.expert_tree_item_layout, null);
                holder.textView = view.findViewById(R.id.title);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            if ("tree".equals(tag)) {
                ExpertTreeModel.DataBean.ChildrenBean childrenBean = (ExpertTreeModel.DataBean.ChildrenBean) listAll.get(i).getData();
                holder.textView.setText(childrenBean.getName().trim());
            } else if ("".equals(tag)) {
                AddressModel addressModel = (AddressModel) listAll.get(i).getData();
                holder.textView.setText(addressModel.getName().trim());
            }
            return view;
        }

        private class ViewHolder {
            TextView textView;
            View bg;
        }
    }

    public interface ItemOnClickListener {
        void getContent(String name, String uuid);
    }

    public void setContent(SelectPopupWindowUtil.ItemOnClickListener listener) {
        this.itemOnClickListener = listener;
    }

    class BaseModel {
        private Object data;

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }
    }
}
